#include "entity.h"
#include "images_diffuse.h"
#include "bullet.h"
#include "hitbox.h"
#include "tag.h"
#include "explosions.h"
#include "enemy.h"
#include "hitpoint.h"

#define PERIOD 20

declare_body(triangle_ship, {
	size_t period;
})

declare_tick(triangle_ship, {
	if(self->period == 0) {
		make_enemy_bullet(
			get_animator_c(&sprite_triangle_bullet, 2, vxy(2.5f, 0)),
			parent->position,
			vxy(-2, -1),
			NULL
		);
		make_enemy_bullet(
			get_animator_c(&sprite_triangle_bullet, 2, vxy(2.5f, 0)),
			parent->position,
			vxy(0, -3),
			NULL
		);
		make_enemy_bullet(
			get_animator_c(&sprite_triangle_bullet, 2, vxy(2.5f, 0)),
			parent->position,
			vxy(2, -1),
			NULL
		);
		self->period = PERIOD;
	}
	else --self->period;
})

declare_draw(triangle_ship, { })

declare_head(triangle_ship)

entity *make_triangle(vec location, int health) {
	entity *ent = ent_new();
	ent->position = vxy(location.x, 250);
	ent->tag = TAG_ENEMY;
	
	triangle_ship *b = c_new(triangle_ship);
	b->period = PERIOD;
	
	add_hitpoint_t(ent, vxy(-14, 6), 16, health);
	add_hitpoint_t(ent, vxy( 14, 6), 15, health);
	add_hitpoint_t(ent, vxy(-11, 6), 14, health);
	add_hitpoint_t(ent, vxy( 11, 6), 13, health);
	add_hitpoint_t(ent, vxy( -8, 6), 12, health);
	add_hitpoint_t(ent, vxy(  8, 6), 11, health);
	add_hitpoint_t(ent, vxy( -5, 6), 10, health);
	add_hitpoint_t(ent, vxy(  5, 6), 9, health);
	add_hitpoint_t(ent, vxy( -2, 6), 8, health);
	add_hitpoint_t(ent, vxy(  2, 6), 7, health);
	add_hitpoint_t(ent, vxy( -8, 2), 6, health);
	add_hitpoint_t(ent, vxy(  8, 2), 5, health);
	add_hitpoint_t(ent, vxy( -5, 2), 4, health);
	add_hitpoint_t(ent, vxy(  5, 2), 3, health);
	add_hitpoint_t(ent, vxy( -2, 2), 2, health);
	add_hitpoint_t(ent, vxy(  2, 2), 1, health);
	
	add_component(ent, get_animator_c(&sprite_triangle_ship, 1, vxy(16, 9)));
	add_component(ent, b);
	add_component(ent, target_loc(location));
	
	add_hitbox(ent, -16, 0, 32, 5);
	
	return ent;
}